<template>
	<view class="login">
		<view class="login-box">
			<uni-forms ref="baseForm" :modelValue="loginData" labelWidth="150rpx" class='login-form'>
				<uni-forms-item label="账号" required>
					<uni-easyinput v-model="loginData.account" placeholder="请输入账号" />
				</uni-forms-item>
				<uni-forms-item label="密码" required>
					<uni-easyinput v-model="loginData.password" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>
			<button class="login-btn" @click="handleLogin">登录</button>
			<view class="login-help">
				<view @click="cancel">清除数据？</view>
				<view class="login-register" @click="toRegister">
					注册
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//提示消息
				messageText: '',
				msgType: '',
				//登录数据
				loginData: {
					account: '',
					password: '',
					identity: 0,
				},
				//登录方式
				range: [{
						identity: 0,
						text: "用户"
					},
					{
						identity: 1,
						text: "医生"
					},
					{
						identity: 2,
						text: "管理员"
					},
				],
				islogin: false,
				setInfo: true
			}
		},
		created() {
			// let currentUser = {}
			// let adminData = [{
			// 	identity: 2,
			// 	account: 'admin',
			// 	password: 'admin'
			// }]
			// let userManagement = [{
			// 	name: '张三',
			// 	age: '21',
			// 	sex: 0,
			// 	mobilePhone: '13654987621',
			// 	IDNumber: '432169875164321469',
			// 	email: '1264973168@qq.com',
			// 	identity: 0,
			// 	account: 'user',
			// 	password: 'user'
			// }, {
			// 	name: '李斯',
			// 	age: '26',
			// 	sex: 1,
			// 	mobilePhone: '13654987621',
			// 	IDNumber: '416597651369871666',
			// 	email: '2649873165@qq.com',
			// 	identity: 0,
			// 	account: 'user1',
			// 	password: 'user1'
			// }]
			// let doctorManagement = [{
			// 	// name: '张明智',
			// 	age: '32',
			// 	identity: 1,
			// 	account: 'doctor',
			// 	password: 'doctor',
			// }]
			// uni.clearStorageSync()
			// if (uni.getStorageSync('currentUser') === '') {
			// 	uni.setStorageSync('currentUser', currentUser);
			// }

			// if (uni.getStorageSync('adminData') === '') {
			// 	uni.setStorageSync('adminData', adminData);
			// }

			// if (uni.getStorageSync('userManagement') === '') {
			// 	uni.setStorageSync('userManagement', userManagement);
			// }

			// if (uni.getStorageSync('doctorManagement') === '') {
			// 	uni.setStorageSync('doctorManagement', doctorManagement);
			// }

			// console.log(this.$store.state)
			// uni.setStorageSync('userManagement', data)
			// console.log(uni.getStorageSync('userManagement'));
		},
		methods: {
			cancel() {
				// uni.showToast({
				// 	title: '数据已清除',
				// 	duration: 1000,
				// 	success() {
				// 		uni.clearStorageSync()
				// 		uni.redirectTo({
				// 			url: "/pages/login/index"
				// 		})
				// 	}
				// })
			},
			handleLogin() {
				this.islogin = false
				let allData = [...this.$store.state.userManagement, ...this.$store.state
					.doctorManagement,
					...this.$store.state.adminData
				]
				// console.log(allData);
				allData.forEach((item, index) => {
					if (this.loginData.account == item.account && this.loginData.password == item.password) {
						if (item.identity == 0) {
							this.loginSuccess(item)
							setTimeout(() => {
								this.loginData.account = ''
								this.loginData.password = ''
								this.loginData.identity = 0
								uni.redirectTo({
									url: '/pages/user/index'
								})
							}, 1000)
						} else if (item.identity == 1) {
							this.loginSuccess(item)
							setTimeout(() => {
								this.loginData.account = ''
								this.loginData.password = ''
								this.loginData.identity = 0
								uni.redirectTo({
									url: '/pages/doctor/index'
								})
							}, 1000)

						} else if (item.identity == 2) {
							this.loginSuccess(item)
							setTimeout(() => {
								this.loginData.account = ''
								this.loginData.password = ''
								this.loginData.identity = 0
								uni.redirectTo({
									url: '/pages/admin/index'
								})
							}, 1000)
						} else {
							console.log(item.identity);
						}
					}
				})
				if (this.islogin == false) {
					this.loginError()
				}
			},
			loginSuccess(item) {
				this.islogin = true
				uni.showToast({
					title: '登录成功',
					mask: true,
					duration: 1000,
				})
				this.$store.commit('getLogin', item)
			},
			loginError() {
				uni.showToast({
					title: '账号或密码错误',
					icon: "error",
					duration: 1000,
				})
			},
			toRegister() {
				uni.navigateTo({
					url: '/pages/login/register/index'
				})
			}
		},
	}
</script>

<style lang="scss">
	.login {
		width: 100vw;
		opacity: 0.8;
		background: linear-gradient(white, transparent);

		.login-box {
			padding: 100rpx 0;
		}

		.uni-forms,
		.login-form {
			width: 600rpx;
			margin: 0 auto;
		}

		.login-btn {
			margin-top: 80rpx;
			margin-bottom: 30rpx;
			width: 600rpx;
			height: 80rpx;
			line-height: 80rpx;
		}

		.login-help {
			width: 600rpx;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			font-size: 13px;
		}

		.login-register {
			color: red;
		}
	}
</style>
